/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.dialogs;

import java.io.File;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Rik Bauwens
 */
public class ExtensionFileFilter extends FileFilter
{
        private String description;
        private String extensions[];

        public ExtensionFileFilter(String description, String[] extensions)
        {
                this.description = description;
                this.extensions = (String[]) extensions.clone();
                toLower(this.extensions);
        }

        private void toLower(String array[])
        {
            for(int i=0; i<array.length; i++)
                    array[i] = array[i].toLowerCase();
        }

        @Override
        public boolean accept(File f)
        {
                if(f.isDirectory())
                {
                        return true;
                }
                else
                {
                        String path = f.getAbsolutePath().toLowerCase();
                        for(int i=0; i<extensions.length; i++)
                        {
                                String extension = extensions[i];
                                if(path.endsWith(extension) &&
                                        path.charAt(path.length() - extension.length()-1) == '.')
                                        return true;
                        }
                }
                return false;
        }

        @Override
        public String getDescription()
        {
                return description;
        }
}
